const state={
	//要设置的全局访问的state对象
     stuDialog: false,
	 stuId:'',
	 clueId:'',
     buyrecordeList:[],
     buyproductList:[],
     orderrecordList:[],
	 trailrecordList:[],
	 mealrecordList:[]
    
   };

const getters = { 
	 //实时监听state值的变化(最新状态)
    isShow(state) {  //方法名随意,主要是来承载变化的stuDialog的值
       return state.stuDialog
    },
    getStuId(state,getters){
    	return state.stuId
    },
    getStudentInfo(state){
    	return state.studentInfo;
    },
    getBuyrecordeList(state){
    	return state.buyrecordeList;
    },
    getBuyproductList(state){
    	
    	return state.buyproductList;
    },
    getOrderrecordList(state){
    	
    	return state.orderrecordList;
	},
	getTrailrecordList(state){
    	
    	return state.trailrecordList;
	},
	getMealrecordList(state){
    	
    	return state.mealrecordList;
	},
    
};

const mutations = {
	
    show(state) {   //自定义改变state初始值的方法，这里面的参数除了state之外还可以再传额外的参数(变量或对象);
        state.stuDialog = true;
    },
    hide(state) {  //同上
        state.stuDialog = false;
    },
    setStuId(state,id){    	
    	state.stuId = id;
	},
	setClueId(state,id){    	
    	state.clueId = id;
    },
    setstudentInfo(state,data){
    	state.studentInfo=data;
    },
    setbuyrecordeList(state,data){
    	state.buyrecordeList=data;
    },
    setbuyproductList(state,data){
    	state.buyproductList=data;
    },
    setOrderrecordList(state,data){
    	state.orderrecordList=data;
    },
	setTrailrecordList(state,data){
    	state.trailrecordList=data;
	},
	setMealrecordList(state,data){
		state.mealrecordList=data;
	}
};

const actions = {
    hidestudetailsPanel(context) {  //自定义触发mutations里函数的方法，context与store 实例具有相同方法和属性
        context.commit('hide');
    },
    showstudetailsPanel(context) {  //同上注释
        context.commit('show');
    },
    setstuId(context,id){   	
    	context.commit('setStuId',id);
	},
	setclueId(context,id){   	
    	context.commit('setClueId',id);
    },
    getstudentInfo(context){ 
    	let paras={studentId:state.stuId}
    	
    	//学员报课列表
    	api.getStudentCourlist(paras).then(function(data){
    		
    		console.log(data);
    		if(data.result=="操作成功"){
    			
    			for(let i = 0;i<data.list.length;i++){
    				data.list[i].index = (i+1);
    			}
    			
    			context.commit("setbuyrecordeList",data.list)
    		}
    		
    		return api.getStudentgoods(paras);
    	}).//学员购买商品记录
    	then(function(data){
    	   // console.log(data)
    		if(data.result=="操作成功"){
    			for(let i = 0;i<data.list.length;i++){
    				data.list[i].index = (i+1);
    			}
    			context.commit("setbuyproductList",data.list)
    		}
    		
    		
    		return api.getOrdersignuplist(paras);
		})//订金记录
		.then(function(data){
    		//   console.log(data)
    		if(data.result=="操作成功"){
    			let list = [];
    			//let inx = 0;
    			for(let i = 0;i<data.list.length;i++){
    				
    				if(data.list[i].useEarnestMoney==1){
    					data.list[i].index = (i+1);
    					list.push(data.list[i]);
    				}
    			}
    			context.commit("setOrderrecordList",list)
			}
			
			return api.gettrialRecords({clueId:state.clueId});
		})//试听记录
		.then(function(data){
			if(data.result=="操作成功"){
				let list=data.list;
				context.commit("setTrailrecordList",list);
			}

			return api.getStudentMealsRecords({studentId:state.stuId});
		})//餐费记录
		.then(function(data){
			if(data.result=="操作成功"){
				let mealsList =  data.list.map(function(row){
					row.checked = false;
					let combinename=row.mealsName;
					if(row.useTime){
						let timearr=row.useTime.split('-');
						combinename=row.mealsName+'('+timearr[1]+'月)';
					}
					row.mealsName=combinename;
					return row;
				});
				context.commit("setMealrecordList",mealsList);
			}
		});
    }
   
};

module.exports = {
	  namespaced:true,//用于在全局引用此文件里的方法时标识这一个的文件名
	  state,
	  getters,
	  actions,
	  mutations
}
